package com.jhgk.jhcee.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.jhgk.jhcee.common.service.RedisService;
import com.jhgk.jhcee.data.auth.entity.SysUserInfo;
import com.jhgk.jhcee.system.service.SysUserCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author: xiezhongping
 * @description:
 * @date: 2021/8/25 10:20.
 */
@Service(value = "SysUserCacheServiceImpl")
public class SysUserCacheServiceImpl implements SysUserCacheService {

    @Autowired
    private RedisService redisService;
    @Value("${redis.database}")
    private String REDIS_DATABASE;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.user}")
    private String REDIS_KEY_USER;

    @Override
    public SysUserInfo getUser(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + userId;
        Object o = redisService.get(key);
        return null != o ? BeanUtil.copyProperties(o, SysUserInfo.class) : null;
    }

    @Override
    public Boolean delUser(Long userId) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + userId;
        return redisService.del(key);
    }

    @Override
    public void setUser(SysUserInfo userInfo) {
        String key = REDIS_DATABASE + ":" + REDIS_KEY_USER + ":" + userInfo.getUserId();
        redisService.set(key, userInfo, REDIS_EXPIRE);
    }
}
